package com.shenw.frame.display;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class StatusWindow extends JFrame {
	
	private static final long serialVersionUID = 535451092734053678L;
	public static final int NAME = 0;
	public static final int BLOOD = 1;
	public static final int CARDS = 2;
	public static final int EQUIP = 3;
	public static final int STATUS = 4;
	Row[] rows;
	
	public StatusWindow(int i) {
		rows = new Row[i];
		initFrame(i);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public String setValue(int row, int key, int value) {
		return setValue(row, key, String.valueOf(value));
	}
	
	public String setValue(int row, int key, String value) {
		
		Row r = rows[row - 1];
		JTextField field;
		
		switch (key) {
		case NAME:
			field = r.getName();
			break;
		case BLOOD:
			field = r.getBlood();
			break;
		case CARDS:
			field = r.getCards();
			break;
		case EQUIP:
			field = r.getEquip();
			break;
		case STATUS:
			field = r.getStatus();
			break;
		default:
			field = null;
			break;
		}
		
		if (field != null) {
			String output = field.getText();
			field.setText(value);
			return output;
		} else {
			return null;
		}
	}

	private void initFrame(int i) {
		Box vbox = Box.createVerticalBox();
		for (int n=0; n < i; n++) {
			rows[n] = new Row(n + 1);
			if (n % 2 == 0) {
				rows[n].setBackground(new Color(220, 255, 220));
			}
			vbox.add(rows[n].getBox());
		}
		getContentPane().add(vbox);
		setSize(191, i * 10 + 101);
	}
}

class Row {

	private JTextField index;
	private JTextField name;
	private JTextField blood;
	private JTextField cards;
	private JTextField equip;
	private JTextField status;
	private Box row;
	
	public Row(int idx) {
		row = Box.createHorizontalBox();
		/*
		 * add component
		 */
		index = new JTextField(String.valueOf(idx));
		row.add(index);
		name = new JTextField();
		row.add(name);
		blood = new JTextField();
		row.add(blood);
		cards = new JTextField();
		row.add(cards);
		equip = new JTextField();
		row.add(equip);
		status = new JTextField();
		row.add(status);
		/*
		 * set component attribute
		 */
		setJTextFieldCantEdit(index, name, blood, cards, equip, status);
		setJTextFieldSize(index, 1.4f);
		setJTextFieldSize(name, 4.1f);
		setJTextFieldSize(blood, 1.4f);
		setJTextFieldSize(cards, 2.0f);
		setJTextFieldSize(equip, 5.4f);
		setJTextFieldSize(status, 3.0f);
	}
	
	public void setBackground(Color color) {
		index.setBackground(color);
		name.setBackground(color);
		blood.setBackground(color);
		cards.setBackground(color);
		equip.setBackground(color);
		status.setBackground(color);
	}

	public Box getBox() {
		return row;
	}

	private void setJTextFieldSize(JTextField tf, float width) {
		tf.setPreferredSize(new Dimension((int)(width * 10), 10));
	}

	private void setJTextFieldCantEdit(JTextField... tfs) {
		for (JTextField tf : tfs)
			tf.setEditable(false);
	}
	
	public JTextField getName() {
		return name;
	}

	public JTextField getBlood() {
		return blood;
	}

	public JTextField getCards() {
		return cards;
	}

	public JTextField getEquip() {
		return equip;
	}

	public JTextField getStatus() {
		return status;
	}

	public JTextField getIndex() {
		return index;
	}
}